<template>
  <div class="togetherbody">
    <div class="groupbuy_cate">
      <div class="groupbuy_cate_top">
        <div v-for="(cate, i) in cateList" :key="i" :class="active == cate.id ? 'groupbuy_active' : ''">
          <p>{{ cate.title }}</p>
          <p>{{ cate.sub_title }}</p>
          <p></p>
        </div>
      </div>
    </div>
    <div class="one_shop_item1" v-for="(item, i) in shoplist" :key="i">
      <div class="one_shop_item1_left">
        <img :src="$fnc.getImgUrl(item.piclink)" alt />
      </div>
      <div class="one_shop_item1_right">
        <p class="van-ellipsis">{{ item.title }}</p>
        <p class="van-ellipsis" v-if="item.sub_title">{{ item.sub_title }}</p>

        <div>
          <p>
            <span>已售{{
                Number(item.real_sale) + Number(item.invent_sale) || 0
              }}件</span>
            <span class="price_regular">
              <small>￥</small>
              <b>{{ get_int_dec(item.price, "int") }}</b>
              <i>{{ get_int_dec(item.price, "dec") }}</i>
            </span>
          </p>
          <p>参与拼购</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      shoplist: [],
      active: 0,
      cateList: []
    };
  },
  props: {
    info: {
      type: Object,
      default: () => ({})
    }
  },
  components: {},
  created () {
    this.getcate();
  },
  methods: {
    getcate () {
      var params = {};
      params.page = 1;
      params.page_size = 50;
      this.$api.getGroupbuy.getGroupbuyCate(params).then(res => {
        if (res.data.code == 200) {
          this.cateList = res.data.result.data;
          if (this.cateList.length > 0) {
            this.active = this.cateList[0].id;
            this.$api.getShops
              .get_groupbuyshop({ id: this.cateList[0].id })
              .then(res2 => {
                if (res2.data.code == 200) {
                  this.shoplist = res2.data.result;
                }
              });
          }
        } else {
          this.$fnc.alerterror(res.data.result);
        }
      });
    },
    get_int_dec (num, type) {
      // @type  int-整数部分 ,dec - 小数部分
      num = num + "";
      num = num.toString();
      var decimal = 2;
      decimal = decimal >= 2 && decimal < 6 ? 2 : decimal;
      decimal = decimal == "0" ? 0 : decimal || 2;
      let index = num.indexOf(".");
      if (index !== -1) {
        num = num.substring(0, decimal + index + 1);
      } else {
        num = num.substring(0);
      }
      num = parseFloat(num).toFixed(decimal);
      if (type == "int") {
        return parseInt(num);
      } else if (type == "dec") {
        return "." + num.toString().split(".")[1] || ".00";
      }
    }
  }
};
</script>
<style lang="less" scoped>
.togetherbody {
  width: 94%;
  margin: 0 auto;
  padding-bottom: 15px;
}
.one_shop_item1 {
  width: 100%;
  height: 120px;
  margin: 10px auto 0 auto;
  background-color: #ffffff;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
  border-radius: 5px;
  line-height: 1.2;
  font-size: 14px;
  .one_shop_item1_left {
    width: 35%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    img {
      width: 100%;
      height: 100%;
      border-radius: 5px 0 0 5px;
    }
  }
  .one_shop_item1_right {
    width: 65%;
    height: 100%;
    padding: 10px 0 10px 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    > p {
      width: 100%;
    }
    > p:nth-child(2) {
      width: 85%;
      font-size: 12px;
      background: #fff5e9;
      color: #c09a71;
      margin-left: 5px;
      padding: 5px 5px;
    }
    > div {
      font-weight: bold;
      font-size: 20px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      > p:nth-of-type(1) {
        display: flex;
        flex-flow: column;
        justify-content: flex-start;
        align-items: flex-start;
        > span {
          width: 100%;
        }
        > span:nth-of-type(1) {
          color: #f9443f;
          font-size: 10px;
        }
        .price_regular {
          > small {
            font-size: 10px;
            font-weight: bold;
          }
          > b {
            font-size: 18px;
          }
          > i {
            font-size: 10px;
            font-weight: normal;
            font-style: normal;
          }
        }
      }
      > p:nth-of-type(2) {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 75px;
        height: 25px;
        font-size: 13px;
        border-radius: 20px;
        color: #ffffff;
        background: #ff0135;
      }
    }
  }
}

.groupbuy_cate {
  width: 100%;
  height: 100%;
  background: transparent;
  padding: 15px 0 0 0;
  margin: 0;
  .groupbuy_cate_top {
    width: 100%;

    display: -webkit-box;
    overflow-y: hidden;
    > div {
      position: relative;
      width: 25%;
      line-height: 1.5;
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      text-align: center;
      > p {
        width: 100%;
      }
      > p:nth-of-type(1) {
        color: #3a4658;
        font-size: 16px;
        font-weight: bold;
      }
      > p:nth-of-type(2) {
        color: #999999;
        font-size: 12px;
        padding-bottom: 10px;
      }
      > p:nth-of-type(3) {
        position: absolute;
        bottom: 0;
        width: 50%;
        height: 0;
        border: 1px solid transparent;
      }
    }
    .groupbuy_active {
      > p:nth-of-type(1) {
        color: #f21551;
      }
      > p:nth-of-type(2) {
        color: #f21551;
      }
      > p:nth-of-type(3) {
        border: 1px solid #f21551;
      }
    }
  }
}
</style>
